package com.itheima.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.OrderSettingService;
import com.itheima.contant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /*将 日期与可预约人数 传入后台，并将数据插入到数据库*/
    @Override
    public void editOrderSetting(OrderSetting orderSetting) {
        //先判断预约设置列表有没有这一天的预约信息
        Integer osObj = orderSettingDao.existOrderSetting(orderSetting.getOrderDate());
        if (osObj != 0) {
            //有就进行更新
            orderSettingDao.updateOrderSetting(orderSetting);

        } else {
            //没有就插入新的预约信息
            orderSettingDao.insertOrderSetting(orderSetting);
        }
    }

    /*批量导入预约数据 TODO : 参数名要和前端传过来的文件名一致，切记 ！*/
    @Override
    public void upload(List<OrderSetting> OrderSettingList) throws IOException {
        for (OrderSetting setting : OrderSettingList) {
                editOrderSetting(setting);
        }
    }

    /*查询指定月份预约信息列表*/
    @Override
    public List<OrderSetting> queryOrderSetting(String date) {
        HashMap<String, String> map = new HashMap<>();
        String beginData = date + "-01";  //每月开始日期
        String endData = date + "-31";    //每月结束日期
        map.put("beginData",beginData);
        map.put("endData",endData);

        /*从数据库中查询出当月的预约信息*/
        List<OrderSetting> list = orderSettingDao.queryOrderSetting(map);
        return list;
    }
}
